package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.applet.utils.*;
import com.yunfeisoft.business.model.PmsProductCategory;
import com.yunfeisoft.business.service.inter.PmsProductCategoryService;
import com.yunfeisoft.enumeration.YesNoEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductCategoryController
 * Description: 商品分类表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class PmsProductCategoryController extends BaseController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    /**
     * 添加商品分类表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(PmsProductCategory record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "name", "分类名称为空");
        validator.zIndex(request, "sort", "排序不合法");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (Constants.ROOT.equals(record.getParentId())) {
            record.setLevel(1);
        } else {
            record.setLevel(2);
        }

        record.setProductCount(0);
        pmsProductCategoryService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改商品分类表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(PmsProductCategory record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "name", "分类名称为空");
        validator.zIndex(request, "sort", "排序不合法");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (Constants.ROOT.equals(record.getParentId())) {
            record.setLevel(1);
        } else {
            record.setLevel(2);
        }
        pmsProductCategoryService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询商品分类表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        PmsProductCategory record = pmsProductCategoryService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询商品分类表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String parentId = ServletRequestUtils.getStringParameter(request, "parentId", null);
        String name = ServletRequestUtils.getStringParameter(request, "name", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);
        params.put("parentId", parentId);

        Page<PmsProductCategory> page = pmsProductCategoryService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 分页查询商品分类表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/queryList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response queryList(HttpServletRequest request, HttpServletResponse response) {
        //Map<String, Object> params = new HashMap<>();
        List<PmsProductCategory> list = pmsProductCategoryService.queryList(null);
        return ResponseUtils.success(list);
    }

    /**
     * 批量删除商品分类表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductCategoryService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 查询一级分类列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/queryTopList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response queryTopList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", Constants.ROOT);
        List<PmsProductCategory> list = pmsProductCategoryService.queryList(params);
        return ResponseUtils.success(list);
    }

    /**
     * 显示在导航栏
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/showNavStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response showNavStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductCategoryService.modifyNavStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("保存成功");
    }

    /**
     * 不显示在导航栏
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/hideNavStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response hideNavStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductCategoryService.modifyNavStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("保存成功");
    }

    /**
     * 显示在移动端
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/showStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response showStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductCategoryService.modifyShowStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("保存成功");
    }

    /**
     * 不显示在移动端
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductCategory/hideStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response hideStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductCategoryService.modifyShowStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("保存成功");
    }
}
